package com.shaoyu.elasticsearch.service;

import com.shaoyu.elasticsearch.entity.ElasticsearchIndex;
import com.shaoyu.elasticsearch.entity.Product;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.MapUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Service
public class ElasticSearchSearchServiceImpl implements ElasticSearchSearchService{

    private Client client = ElasticSearchClientService.getNormalTransportClient();
    /**匹配查询（match query）*/
    private static final String MATCH_QUERY = "match_query";
    /**多字段查询（multi_match query）*/
    private static final String MULTI_MATCH_QUERY = "multi_match_query";
    /**常用术语查询（common_terms query）*/
    private static final String COMMON_TERMS_QUERY = "common_terms_query";
    /**查询语句查询（query_string query）*/
    private static final String QUERY_STRING_QUERY = "query_string_query";
    /**简单查询语句（simple_query_string）*/
    private static final String SIMPLE_QUERY_STRING = "simple_query_string";


    private QueryBuilder getQuery(Map<String,Object> param){
        if(param.get(MATCH_QUERY)!=null){
            Map<String,String> p = (Map<String, String>) param.get(MATCH_QUERY);
            if(!MapUtils.isEmpty(p)){
                for (Map.Entry<String,String>  entry: p.entrySet()){
                    return QueryBuilders.matchQuery(entry.getKey(),entry.getValue());
                }
            }
        }else if(param.get(MULTI_MATCH_QUERY)!=null){
            Map<String,String[]> p = (Map<String, String[]>) param.get(MULTI_MATCH_QUERY);
            if(!MapUtils.isEmpty(p)){
                for (Map.Entry<String,String[]>  entry: p.entrySet()){
                    return QueryBuilders.multiMatchQuery(entry.getKey(),entry.getValue());
                }
            }
        }else if(param.get(COMMON_TERMS_QUERY)!=null){

        }else if(param.get(QUERY_STRING_QUERY)!=null){

        }else if(param.get(SIMPLE_QUERY_STRING)!=null){

        }
        return QueryBuilders.termQuery("","");
    }


    public SearchResponse search(List<ElasticsearchIndex> indexList, Product product){
        String [] indexArr = new String[indexList.size()];
        String [] typeArr = new String[indexList.size()];
        for (int i= 0;i<indexList.size();i++) {
            typeArr[i]=indexList.get(i).getType();
            indexArr[i]=indexList.get(i).getIndex();
        }

        QueryBuilder query = QueryBuilders.termQuery("name",product.getName());// Query 查询条件
        QueryBuilder postFilter = QueryBuilders.rangeQuery("price").from(product.getPrice()).to(product.getPrice()); // Filter 过滤


        SearchResponse response = client.prepareSearch(indexArr)
                .setTypes(typeArr)
//                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .setQuery(query)                 // Query 查询条件
                .setPostFilter(postFilter)     // Filter 过滤
                .setFrom(0).setSize(60).setExplain(true)
                .get();
        return response;
    }

    @Override
    public SearchResponse search(Map param) {






        return null;
    }
    public SearchResponse search(ElasticsearchIndex index){

        try {
            SearchRequest request = new SearchRequest();

            SearchResponse response = client.search(request).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return null;
    }
}
